package com.amlyc.ch04;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadLocalDemo {
    private static ThreadLocal<SimpleDateFormat> sdf = new ThreadLocal<>();
    public static class ParseDate implements Runnable{

        @Override
        public void run() {
            if (sdf.get()==null) {
                sdf.set(new SimpleDateFormat("yyyy-MM-dd"));

            }
            try {
                Date t = sdf.get().parse("2015-12-12");
                System.out.println(Thread.currentThread().getName()+" "+t);
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
    }

    public static void main(String[] args) throws InterruptedException {
        ExecutorService service = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 1000; i++) {
            service.execute(new ParseDate());
        }

        /*for (int i = 0; i < 10; i++) {
            new Thread(new ParseDate()).start();

        }
        Thread.sleep(2000);*/


    }

}
